<template>
  <div v-show="visible" class="modal-overlay">
    <div class="modal-container">
      <div class="modal-header">
        <h3 class="modal-title">选择题库</h3>
        <button class="modal-close-btn" @click="close">
          <i class="fas fa-times"></i>
        </button>
      </div>

      <div class="modal-content-wrapper">
        <!-- 左侧题库选择部分 -->
        <div class="bank-selection-panel">
          <div class="modal-search">
            <div class="search-input">
              <i class="fas fa-search"></i>
              <input
                type="text"
                v-model="searchQuery"
                placeholder="搜索题库..."
                @input="handleSearch"
              />
            </div>
          </div>

          <div
            class="bank-list-container"
            ref="bankListContainer"
            @scroll="handleScrollWithDebounce"
          >
            <div
              v-if="localLoading && localBanks.length === 0"
              class="loading-state"
            >
              <div class="spinner"></div>
              <p>加载题库中...</p>
            </div>

            <div
              v-else-if="localBanks.length === 0 && !localLoading"
              class="empty-state"
            >
              <i class="fas fa-inbox"></i>
              <p>未找到匹配的题库</p>
            </div>

            <div v-else class="bank-list">
              <div
                v-for="(bank, index) in localBanks"
                :key="`bank-${bank.bankId}-${index}`"
                class="bank-item"
                :class="{ selected: selectedBankId === bank.bankId }"
                @click="selectBank(bank)"
              >
                <div class="bank-info">
                  <h4 class="bank-name">{{ bank.bankName }}</h4>
                  <p class="bank-meta">
                    <span class="question-count">
                      <i class="fas fa-question-circle"></i>
                      {{ bank.totalQuestionNum || 0 }} 题
                    </span>
                    <span
                      class="difficulty"
                      :style="getDifficultyStyle(bank.avgDiffculty)"
                    >
                      {{ getDifficultyText(bank.avgDiffculty) }}
                    </span>
                  </p>
                  <!-- 题型统计信息 -->
                  <div class="type-stats" v-if="bankQuestionTypes[bank.bankId]">
                    <div
                      v-for="type in getBankAvailableTypes(bank.bankId)"
                      :key="type.value"
                      class="type-stat-item"
                    >
                      <span
                        class="type-badge"
                        :style="{ backgroundColor: getTypeColor(type.value) }"
                      >
                        <i :class="getTypeIcon(type.value)"></i>
                      </span>
                      <span class="type-count">{{
                        getBankTypeCount(bank.bankId, type.value)
                      }}</span>
                    </div>
                  </div>
                </div>
                <div class="bank-selection">
                  <div
                    class="radio"
                    :class="{ checked: selectedBankId === bank.bankId }"
                  >
                    <div
                      v-if="selectedBankId === bank.bankId"
                      class="radio-dot"
                    ></div>
                  </div>
                </div>
              </div>

              <div v-if="localLoading" class="loading-more">
                <div class="spinner"></div>
                <span>加载更多...</span>
              </div>

              <div
                v-if="!localPagination.hasMore && localBanks.length > 0"
                class="no-more"
              >
                <span>— 没有更多题库了 —</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 右侧题型分布和难度分布部分 -->
        <div class="question-config-panel" v-if="selectedBankId">
          <!-- 题型分布（美观卡片式） -->
          <div class="type-distribution-section">
            <div class="info-header">
              <i class="fas fa-chart-pie"></i>
              <span>题型分布</span>
            </div>
            <div class="type-distribution-list">
              <div
                v-for="type in filteredTypeDistributions"
                :key="type.value"
                class="type-distribution-item"
              >
                <div class="type-icon" :style="{ backgroundColor: type.color }">
                  <i :class="type.icon"></i>
                </div>
                <div class="type-info">
                  <span class="type-name">{{ type.label }}</span>
                  <span class="type-count"
                    >{{ typeDistribution[type.value] }} 题</span
                  >
                </div>
                <div class="type-percentage">
                  {{ getTypePercentage(type.value) }}%
                </div>
              </div>
            </div>
          </div>
          <!-- 难度分布 -->
          <div class="difficulty-distribution-section">
            <div class="info-header">
              <i class="fas fa-signal"></i>
              <span>难度分布</span>
            </div>
            <div class="difficulty-distribution">
              <div class="difficulty-bar">
                <div
                  class="difficulty-segment easy"
                  :style="{
                    width: calculateDifficultyPercentage('easy') + '%',
                  }"
                  :title="
                    '简单: ' + calculateDifficultyPercentage('easy') + '%'
                  "
                ></div>
                <div
                  class="difficulty-segment medium"
                  :style="{
                    width: calculateDifficultyPercentage('medium') + '%',
                  }"
                  :title="
                    '中等: ' + calculateDifficultyPercentage('medium') + '%'
                  "
                ></div>
                <div
                  class="difficulty-segment hard"
                  :style="{
                    width: calculateDifficultyPercentage('hard') + '%',
                  }"
                  :title="
                    '困难: ' + calculateDifficultyPercentage('hard') + '%'
                  "
                ></div>
              </div>
              <div class="difficulty-legend">
                <div class="legend-item">
                  <span class="legend-dot easy"></span>
                  <span class="legend-text">简单</span>
                  <span class="legend-percentage"
                    >{{ calculateDifficultyPercentage("easy") }}%</span
                  >
                </div>
                <div class="legend-item">
                  <span class="legend-dot medium"></span>
                  <span class="legend-text">中等</span>
                  <span class="legend-percentage"
                    >{{ calculateDifficultyPercentage("medium") }}%</span
                  >
                </div>
                <div class="legend-item">
                  <span class="legend-dot hard"></span>
                  <span class="legend-text">困难</span>
                  <span class="legend-percentage"
                    >{{ calculateDifficultyPercentage("hard") }}%</span
                  >
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 无选中题库时的空状态 -->
        <div class="empty-config-panel" v-if="!selectedBankId">
          <div class="empty-state-content">
            <i class="fas fa-hand-point-left"></i>
            <p>请先从左侧选择一个题库</p>
          </div>
        </div>
      </div>

      <div class="modal-footer">
        <div class="selected-info">
          <template v-if="selectedBankId">
            已选择 <span class="selected-count">1</span> 个题库
            <span v-if="selectedQuestionTypes.length > 0">
              ，{{ selectedQuestionTypes.length }} 种题型
            </span>
            <span v-if="totalSelectedQuestions > 0">
              ，共 {{ totalSelectedQuestions }} 题
            </span>
          </template>
          <template v-else>请选择一个题库</template>
        </div>
        <div class="footer-actions">
          <button class="btn btn-cancel" @click="close">取消</button>
          <button
            class="btn btn-confirm"
            @click="confirm"
            :disabled="!selectedBankId"
          >
            确认选择
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getQuestionBankList, getQuestionAll } from "@/api/question";

export default {
  name: "SelectBankModal",
  data() {
    return {
      visible: false,
      searchQuery: "",
      selectedBankId: null,
      loadingError: false,
      // 新增：本地题库列表管理，不依赖vuex
      localBanks: [],
      localLoading: false,
      localPagination: {
        currentPage: 1,
        pageSize: 10,
        hasMore: true,
      },
      // 题型定义（数字字符串value，带icon和color）
      questionTypes: [
        {
          value: "0",
          label: "单选题",
          icon: "fas fa-dot-circle",
          color: "#4361EE",
        },
        {
          value: "1",
          label: "多选题",
          icon: "fas fa-check-double",
          color: "#3A0CA3",
        },
        {
          value: "2",
          label: "填空题",
          icon: "fas fa-pencil-alt",
          color: "#F59E0B",
        },
        {
          value: "3",
          label: "判断题",
          icon: "fas fa-balance-scale",
          color: "#10B981",
        },
        {
          value: "4",
          label: "简答题",
          icon: "fas fa-comment-dots",
          color: "#EF4444",
        },
      ],
      // 题型颜色配置
      typeColors: {
        single: "#4361EE",
        multiple: "#3A0CA3",
        truefalse: "#10B981",
        fillblank: "#F59E0B",
        shortanswer: "#EF4444",
      },
      selectedQuestionTypes: [], // 选中的题型
      typeSelectionCounts: {}, // 每种题型选择的数量
      difficultyLevels: {
        1: { text: "简单", color: "#10B981" },
        2: { text: "简单", color: "#10B981" },
        3: { text: "简单", color: "#10B981" },
        4: { text: "中等", color: "#10B981" },
        5: { text: "中等", color: "#F59E0B" },
        6: { text: "中等", color: "#F59E0B" },
        7: { text: "中等", color: "#EF4444" },
        8: { text: "困难", color: "#EF4444" },
        9: { text: "困难", color: "#EF4444" },
        10: { text: "困难", color: "#EF4444" },
      },
      scrollDebounce: null,
      scrollOptions: {
        threshold: 20,
        debounceTime: 300,
      },
      // 题库题型统计信息
      bankQuestionTypes: {},
      totalQuestionSlider: 10,
      selectedDifficulty: "medium",
      // 难度配置
      difficultyConfig: {
        easy: { min: 1, max: 4, color: "#10B981", label: "简单" },
        medium: { min: 5, max: 7, color: "#F59E0B", label: "中等" },
        hard: { min: 8, max: 10, color: "#EF4444", label: "困难" },
      },
      // 题型分配比例
      typeDistribution: {},
      // 难度分布初始化，保证响应式
      difficultyDistribution: { easy: 0, medium: 0, hard: 0 },
    };
  },
  computed: {
    // 不再从Vuex获取状态
    shouldAutoSendBankId() {
      return this.$store.state.ai && this.$store.state.ai.shouldAutoSendBankId;
    },

    // 当前题库可用的题型
    availableQuestionTypes() {
      if (!this.selectedBankId) return [];

      // 获取当前题库的题型统计
      const bankTypeStats = this.bankQuestionTypes[this.selectedBankId] || {};

      return this.questionTypes.filter((type) => {
        return bankTypeStats[type.value] > 0;
      });
    },

    // 计算当前选择的题目总数
    totalSelectedQuestions() {
      if (!this.selectedBankId || this.selectedQuestionTypes.length === 0)
        return 0;

      let total = 0;

      Object.keys(this.typeSelectionCounts).forEach((type) => {
        if (this.selectedQuestionTypes.includes(type)) {
          total += this.typeSelectionCounts[type] || 0;
        }
      });

      return total;
    },

    // 获取选中的题库名称
    getSelectedBankName() {
      const bank = this.localBanks.find(
        (b) => b.bankId === this.selectedBankId
      );
      return bank ? bank.bankName : "";
    },

    // 获取选中的题库总题目数
    getSelectedBankTotalQuestions() {
      const bank = this.localBanks.find(
        (b) => b.bankId === this.selectedBankId
      );
      return bank ? bank.totalQuestionNum : 0;
    },

    // 获取选中的题库平均难度
    getSelectedBankDifficulty() {
      const bank = this.localBanks.find(
        (b) => b.bankId === this.selectedBankId
      );
      return bank ? bank.avgDiffculty : 0;
    },
    filteredTypeDistributions() {
      return this.questionTypes.filter(
        (type) => this.typeDistribution[type.value]
      );
    },
    // 难度分布百分比
    totalDifficultyCount() {
      const d = this.difficultyDistribution || { easy: 0, medium: 0, hard: 0 };
      return d.easy + d.medium + d.hard;
    },
    calculateDifficultyPercentage() {
      return (diff) => {
        const total = this.totalDifficultyCount;
        if (!total) return 0;
        return Math.round((this.difficultyDistribution[diff] / total) * 100);
      };
    },
  },
  methods: {
    open() {
      this.visible = true;
      this.selectedBankId = null;
      this.selectedQuestionTypes = [];
      this.typeSelectionCounts = {};
      this.searchQuery = "";
      this.totalQuestionSlider = 10;
      this.selectedDifficulty = "medium";

      // 重置本地分页和数据
      this.localBanks = [];
      this.localPagination = {
        currentPage: 1,
        pageSize: 10,
        hasMore: true,
      };

      // 直接从API获取数据，不使用Vuex
      this.fetchLocalBanks();
    },

    // 获取题库数据的本地方法
    async fetchLocalBanks() {
      if (this.localLoading || !this.localPagination.hasMore) return;

      this.localLoading = true;
      try {
        // 从API获取题库列表
        const response = await getQuestionBankList({
          page: this.localPagination.currentPage,
          pageSize: this.localPagination.pageSize,
          bankName: this.searchQuery.trim(), // 使用本地搜索关键词
        });

        // 获取结果
        const banks = response?.data?.data?.rows || [];
        const totalItems = response?.data?.data?.total || 0;

        // 如果是第一页，替换数据；否则追加
        if (this.localPagination.currentPage === 1) {
          this.localBanks = banks;
        } else {
          // 过滤掉可能重复的题库
          const newBanks = banks.filter(
            (newBank) =>
              !this.localBanks.some(
                (existingBank) => existingBank.bankId === newBank.bankId
              )
          );
          this.localBanks = [...this.localBanks, ...newBanks];
        }

        // 更新是否还有更多数据
        this.localPagination.hasMore =
          banks.length === this.localPagination.pageSize &&
          this.localPagination.currentPage * this.localPagination.pageSize <
            totalItems;

        // 增加页码以备下次加载
        this.localPagination.currentPage++;

        // 如果获取数据后有题库，默认选中第一个
        if (this.localBanks.length > 0 && !this.selectedBankId) {
          this.$nextTick(() => {
            this.selectBank(this.localBanks[0]);
          });
        }
      } catch (error) {
        console.error("获取题库列表失败:", error);
        this.loadingError = true;
      } finally {
        this.localLoading = false;
      }
    },

    loadMoreBanks() {
      if (this.localLoading || !this.localPagination.hasMore) return;

      // 直接使用本地方法加载更多
      this.fetchLocalBanks();
    },

    close(sendCancelMessage = true) {
      // 设置用户输入并发送消息
      if (sendCancelMessage) {
        this.$store.commit("aiPanel/SET_USER_INPUT", "取消选择题库");
        setTimeout(() => {
          this.$store.dispatch("aiPanel/sendMessage", {
            prompt: "取消选择题库",
            agentType: "question",
          });
        }, 100);
      }

      this.visible = false;
      this.$emit("closed");
    },

    confirm() {
      if (!this.selectedBankId) return;

      // 获取选中的题库信息
      const selectedBank = this.localBanks.find(
        (bank) => bank.bankId === this.selectedBankId
      );

      // 构造发送给AI的消息
      const aiMessage = `选择的题库为：${this.selectedBankId}`;

      // 设置用户输入并发送消息
      this.$store.commit("aiPanel/SET_USER_INPUT", aiMessage);
      setTimeout(() => {
        this.$store.dispatch("aiPanel/sendMessage", {
          prompt: aiMessage,
          agentType: "question",
        });
      }, 100);
      this.close(false);
    },

    async selectBank(bank) {
      const oldBankId = this.selectedBankId;
      if (oldBankId === bank.bankId) return;
      this.selectedBankId = bank.bankId;
      try {
        const res = await getQuestionAll({ bankId: bank.bankId });
        const questions = res?.data?.data || [];
        this.updateTypeDistribution(questions);
      } catch (e) {
        this.updateTypeDistribution([]);
      }
    },

    updateTypeDistribution(questions = []) {
      // 统计题型分布和难度分布
      const typeCount = {};
      const difficultyCount = { easy: 0, medium: 0, hard: 0 };
      this.questionTypes.forEach((type) => {
        typeCount[type.value] = 0;
      });
      questions.forEach((q) => {
        const t = String(q.type);
        if (typeCount.hasOwnProperty(t)) {
          typeCount[t]++;
        }
        // 兼容多种难度字段和类型
        let diff = q.difficulty ?? q.diff ?? q.level ?? q.难度;
        if (typeof diff === "string") {
          if (diff === "简单") difficultyCount.easy++;
          else if (diff === "中等") difficultyCount.medium++;
          else if (diff === "困难") difficultyCount.hard++;
          else {
            const num = Number(diff);
            if (num >= 1 && num <= 4) difficultyCount.easy++;
            else if (num >= 5 && num <= 7) difficultyCount.medium++;
            else if (num >= 8 && num <= 10) difficultyCount.hard++;
          }
        } else if (typeof diff === "number") {
          if (diff >= 1 && diff <= 4) difficultyCount.easy++;
          else if (diff >= 5 && diff <= 7) difficultyCount.medium++;
          else if (diff >= 8 && diff <= 10) difficultyCount.hard++;
        }
      });
      this.typeDistribution = typeCount;
      this.difficultyDistribution = difficultyCount;
    },

    handleSearch() {
      // 重置本地分页
      this.localPagination = {
        currentPage: 1,
        pageSize: 10,
        hasMore: true,
      };

      // 使用本地搜索获取数据
      this.fetchLocalBanks();

      // 滚动到顶部
      if (this.$refs.bankListContainer) {
        this.$refs.bankListContainer.scrollTop = 0;
      }
    },

    handleScrollWithDebounce(e) {
      if (this.scrollDebounce) {
        clearTimeout(this.scrollDebounce);
      }

      this.scrollDebounce = setTimeout(() => {
        if (!this.localLoading && this.localPagination.hasMore) {
          const container = e.target;
          const { scrollHeight, scrollTop, clientHeight } = container;
          if (scrollHeight - scrollTop - clientHeight <= 100) {
            this.loadMoreBanks();
          }
        }
      }, 200);
    },

    getDifficultyText(level) {
      const numLevel = parseInt(level);
      if (isNaN(numLevel)) return "未设置";
      return this.difficultyLevels[numLevel]?.text || "未设置";
    },

    getDifficultyStyle(level) {
      const numLevel = parseInt(level);
      if (isNaN(numLevel)) {
        return {
          color: "#94A3B8",
          backgroundColor: "#F1F5F9",
        };
      }

      const color = this.difficultyLevels[numLevel]?.color || "#94A3B8";
      return {
        color: "white",
        backgroundColor: color,
      };
    },

    getTypePercentage(type) {
      const total = Object.values(this.typeDistribution).reduce(
        (sum, n) => sum + n,
        0
      );
      if (!total) return 0;
      return Math.round((this.typeDistribution[type] / total) * 100);
    },
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        document.addEventListener("keydown", this.handleKeyDown);
      } else {
        document.removeEventListener("keydown", this.handleKeyDown);
      }
    },
  },
  created() {
    // 初始化逻辑可以在这里添加，目前不需要
  },
};
</script>

<style lang="less" scoped>
@import "@/styles/variables.less";

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(3px);
}

.modal-container {
  background-color: white;
  border-radius: 8px;
  width: 90%;
  max-width: 900px; // 增加宽度以适应左右布局
  max-height: 80vh;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
    0 10px 10px -5px rgba(0, 0, 0, 0.04);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid @gray-200;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, @primary, @secondary);
  color: white;

  .modal-title {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
  }

  .modal-close-btn {
    background: transparent;
    border: none;
    color: white;
    cursor: pointer;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s ease;

    &:hover {
      background: rgba(255, 255, 255, 0.2);
    }

    i {
      font-size: 1rem;
    }
  }
}

// 修改为左右布局
.modal-content-wrapper {
  display: flex;
  overflow: hidden;
  height: 60vh; // 固定高度
}

// 左侧题库选择面板
.bank-selection-panel {
  width: 40%;
  border-right: 1px solid @gray-200;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-search {
  padding: 1rem;
  border-bottom: 1px solid @gray-200;

  .search-input {
    position: relative;

    i {
      position: absolute;
      left: 12px;
      top: 50%;
      transform: translateY(-50%);
      color: @gray-500;
    }

    input {
      width: 100%;
      padding: 0.75rem 1rem 0.75rem 2.5rem;
      border: 1px solid @gray-300;
      border-radius: 8px;
      font-size: 0.875rem;
      transition: all 0.2s ease;

      &:focus {
        outline: none;
        border-color: @primary;
        box-shadow: 0 0 0 3px rgba(@primary, 0.1);
      }

      &::placeholder {
        color: @gray-400;
      }
    }
  }
}

.bank-list-container {
  flex: 1;
  overflow-y: auto;
  padding: 0;
}

.loading-state,
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem 1rem;
  text-align: center;
}

.loading-state {
  .spinner {
    width: 40px;
    height: 40px;
    border: 3px solid @gray-200;
    border-top-color: @primary;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 1rem;
  }

  p {
    color: @gray-600;
    margin: 0;
  }
}

.empty-state {
  i {
    font-size: 2.5rem;
    color: @gray-400;
    margin-bottom: 1rem;
  }

  p {
    color: @gray-600;
    margin: 0;
  }
}

.bank-list {
  padding: 1rem;
}

.bank-item {
  padding: 1rem;
  border-radius: 8px;
  border: 1px solid @gray-200;
  margin-bottom: 0.75rem;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: space-between;

  &:hover {
    border-color: @primary-light;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
      0 2px 4px -1px rgba(0, 0, 0, 0.06);
    transform: translateY(-2px);
  }

  &.selected {
    border-color: @primary;
    background-color: rgba(@primary, 0.05);
  }

  .bank-info {
    flex: 1;
    margin-right: 1rem;

    .bank-name {
      margin: 0 0 0.5rem 0;
      font-size: 1rem;
      font-weight: 600;
      color: @gray-800;
    }

    .bank-meta {
      display: flex;
      align-items: center;
      gap: 1rem;
      margin: 0;
      font-size: 0.875rem;
      color: @gray-600;

      .question-count {
        display: flex;
        align-items: center;
        gap: 0.25rem;

        i {
          color: @primary;
        }
      }

      .difficulty {
        padding: 0.25rem 0.5rem;
        border-radius: 4px;
        font-size: 0.75rem;
        font-weight: 500;
      }
    }

    // 题型统计样式
    .type-stats {
      display: flex;
      flex-wrap: wrap;
      gap: 0.5rem;
      margin-top: 0.75rem;

      .type-stat-item {
        display: flex;
        align-items: center;
        gap: 0.25rem;

        .type-badge {
          width: 1.5rem;
          height: 1.5rem;
          border-radius: 4px;
          display: flex;
          align-items: center;
          justify-content: center;
          color: white;
          font-size: 0.625rem;
        }

        .type-count {
          font-size: 0.75rem;
          color: @gray-600;
        }
      }
    }
  }

  .bank-selection {
    .radio {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      border: 2px solid @gray-300;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: all 0.2s ease;

      &.checked {
        border-color: @primary;
      }

      .radio-dot {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        background-color: @primary;
      }
    }
  }
}

.loading-more {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  color: @gray-600;

  .spinner {
    width: 16px;
    height: 16px;
    border: 2px solid @gray-200;
    border-top-color: @primary;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-right: 0.5rem;
  }
}

.no-more {
  text-align: center;
  padding: 1rem;
  color: @gray-500;
  font-size: 0.875rem;
}

// 右侧题型分布和难度分布部分
.question-config-panel {
  width: 60%;
  padding: 1.5rem;
  overflow-y: auto;
}

.panel-title {
  font-size: 1.125rem;
  color: @gray-700;
  margin: 0 0 1.5rem 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;

  i {
    color: @primary;
  }
}

// 空配置面板（未选择题库时）
.empty-config-panel {
  width: 60%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: @gray-50;
}

.empty-state-content {
  text-align: center;
  padding: 2rem;

  i {
    font-size: 3rem;
    color: @gray-300;
    margin-bottom: 1rem;
  }

  p {
    color: @gray-500;
    font-size: 1.125rem;
  }
}

// 智能参数区域样式
.smart-params-section {
  background-color: @gray-50;
  border-radius: 10px;
  padding: 1.25rem;
  margin-bottom: 1.5rem;
  border: 1px solid @gray-200;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  color: @gray-700;
  margin-bottom: 1.25rem;

  i {
    color: @primary;
  }
}

.section-divider {
  height: 1px;
  background-color: @gray-200;
  margin: 1.5rem 0;
}

.param-item {
  margin-bottom: 1.25rem;

  .param-label {
    font-size: 0.875rem;
    font-weight: 500;
    color: @gray-700;
    margin-bottom: 0.5rem;
  }
}

.slider-container {
  display: flex;
  align-items: center;
  gap: 1rem;

  .range-slider {
    flex: 1;
    -webkit-appearance: none;
    height: 6px;
    background: @gray-300;
    border-radius: 3px;
    outline: none;

    &::-webkit-slider-thumb {
      -webkit-appearance: none;
      width: 18px;
      height: 18px;
      border-radius: 50%;
      background: @primary;
      cursor: pointer;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
      transition: all 0.2s ease;

      &:hover {
        transform: scale(1.1);
      }
    }

    &::-moz-range-thumb {
      width: 18px;
      height: 18px;
      border-radius: 50%;
      background: @primary;
      cursor: pointer;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
      transition: all 0.2s ease;
      border: none;

      &:hover {
        transform: scale(1.1);
      }
    }
  }

  .slider-value {
    font-weight: 600;
    color: @primary;
    min-width: 50px;
    text-align: center;
    border-radius: 4px;
    padding: 0.25rem 0.5rem;
    background-color: rgba(@primary, 0.1);
  }
}

.difficulty-options {
  display: flex;
  gap: 0.75rem;

  .difficulty-option {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.75rem;
    border-radius: 8px;
    border: 1px solid @gray-200;
    background-color: white;
    cursor: pointer;
    transition: all 0.2s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    }

    &.active {
      border-color: @primary;
      background-color: rgba(@primary, 0.05);

      .difficulty-icon {
        transform: scale(1.1);
      }

      span {
        color: @primary;
        font-weight: 600;
      }
    }

    .difficulty-icon {
      font-size: 1.25rem;
      margin-bottom: 0.5rem;
      transition: all 0.2s ease;

      &.easy {
        color: #10b981;
      }

      &.medium {
        color: #f59e0b;
      }

      &.hard {
        color: #ef4444;
      }
    }

    span {
      font-size: 0.875rem;
      color: @gray-700;
    }
  }
}

.smart-config-actions {
  margin-top: 0.5rem;

  .btn-smart-config {
    width: 100%;
    padding: 0.75rem;
    background: linear-gradient(135deg, @primary, darken(@primary, 10%));
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: all 0.2s ease;

    &:hover {
      transform: translateY(-1px);
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
    }

    i {
      font-size: 1rem;
    }
  }
}

.question-types-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.question-type-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem;
  border-radius: 10px;
  border: 1px solid #e2e8f0;
  background-color: #f8fafc;
  transition: all 0.2s ease;
  cursor: pointer;

  &.active {
    background-color: #f0f5ff;
    border-color: #3b82f6;
  }

  &:hover {
    border-color: #93c5fd;
  }

  .type-content {
    display: flex;
    align-items: center;
    gap: 1rem;
  }

  .type-icon-container {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 1rem;
  }

  .type-info {
    display: flex;
    flex-direction: column;

    .type-name {
      font-weight: 600;
      color: #1e293b;
      font-size: 0.9375rem;
    }

    .type-desc {
      font-size: 0.8125rem;
      color: #64748b;
      margin-top: 0.125rem;
    }
  }

  .type-count {
    display: flex;
    align-items: center;
    gap: 0.375rem;

    .btn-count {
      width: 28px;
      height: 28px;
      border-radius: 6px;
      background-color: #f1f5f9;
      border: 1px solid #e2e8f0;
      color: #475569;
      font-size: 1rem;
      font-weight: 500;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: all 0.15s ease;
      user-select: none;
      outline: none; /* 移除点击时的轮廓 */

      &:hover:not([disabled]) {
        background-color: #e2e8f0;
      }

      &:active:not([disabled]) {
        transform: scale(0.96);
        background-color: #cbd5e1;
      }

      &[disabled] {
        opacity: 0.4;
        cursor: not-allowed;
      }
    }

    .count-number {
      min-width: 20px;
      text-align: center;
      font-size: 0.9375rem;
      font-weight: 600;
      color: #334155;
    }
  }
}

.total-count {
  text-align: right;
  padding: 0.75rem 0.5rem;
  color: #1e293b;
  font-size: 0.9375rem;
  font-weight: 500;
  border-top: 1px solid #e2e8f0;
  margin-top: 0.5rem;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid @gray-200;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: @gray-50;

  .selected-info {
    font-size: 0.875rem;
    color: @gray-600;

    .selected-count {
      font-weight: 600;
      color: @primary;
    }
  }

  .footer-actions {
    display: flex;
    gap: 0.75rem;

    .btn {
      padding: 0.5rem 1rem;
      border-radius: 6px;
      font-size: 0.875rem;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.2s ease;
    }

    .btn-cancel {
      background: @gray-100;
      color: @gray-700;
      border: 1px solid @gray-300;

      &:hover {
        background: @gray-200;
      }
    }

    .btn-confirm {
      background: @primary;
      color: white;
      border: none;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);

      &:hover:not([disabled]) {
        background: darken(@primary, 5%);
      }

      &[disabled] {
        opacity: 0.7;
        cursor: not-allowed;
      }
    }
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-content-wrapper {
    flex-direction: column;
    height: auto;
    max-height: 70vh;
  }

  .bank-selection-panel,
  .question-config-panel,
  .empty-config-panel {
    width: 100%;
  }

  .bank-selection-panel {
    max-height: 300px;
    border-right: none;
    border-bottom: 1px solid @gray-200;
  }

  .modal-footer {
    flex-direction: column;
    gap: 1rem;

    .selected-info {
      width: 100%;
      text-align: center;
    }

    .footer-actions {
      width: 100%;

      .btn {
        flex: 1;
      }
    }
  }
}

// 难度选择区域
.difficulty-section {
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid #e2e8f0;
}

.difficulty-options {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;

  .difficulty-option {
    flex: 1;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
    cursor: pointer;
    transition: all 0.2s ease;

    &:hover {
      border-color: #93c5fd;
    }

    &.active {
      background-color: #eff6ff;
      border-color: #3b82f6;

      span {
        color: #1d4ed8;
        font-weight: 500;
      }
    }

    .difficulty-dot {
      width: 20px;
      height: 20px;
      border-radius: 50%;

      &.easy {
        background-color: #10b981;
      }

      &.medium {
        background-color: #f59e0b;
      }

      &.hard {
        background-color: #ef4444;
      }
    }

    span {
      font-size: 0.9375rem;
      color: #475569;
    }
  }
}

.btn-smart-config {
  width: 100%;
  height: 44px;
  background-color: #4f46e5;
  color: white;
  border: none;
  border-radius: 8px;
  font-weight: 500;
  font-size: 0.9375rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  cursor: pointer;
  transition: all 0.2s ease;

  &:hover {
    background-color: #4338ca;
  }

  i {
    font-size: 1rem;
  }
}

// 题库信息面板样式
.bank-info-section,
.type-distribution-section,
.difficulty-distribution-section {
  background-color: white;
  border-radius: 8px;
  padding: 1.25rem;
  margin-bottom: 1.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.info-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  color: @gray-700;
  font-weight: 600;

  i {
    color: @primary;
  }
}

.info-content {
  .info-item {
    display: flex;
    align-items: center;
    margin-bottom: 0.75rem;

    &:last-child {
      margin-bottom: 0;
    }

    .label {
      color: @gray-600;
      width: 80px;
    }

    .value {
      color: @gray-800;
      font-weight: 500;

      &.difficulty-badge {
        padding: 0.25rem 0.75rem;
        border-radius: 4px;
        font-size: 0.875rem;
        color: white;
      }
    }
  }
}

.type-distribution-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.type-distribution-item {
  display: flex;
  align-items: center;
  padding: 0.75rem;
  background-color: @gray-50;
  border-radius: 6px;
  margin-bottom: 0.5rem;
  box-shadow: 0 1px 2px rgba(67, 97, 238, 0.04);
}

.type-icon {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-right: 1rem;
  font-size: 1.2rem;
}

.type-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  .type-name {
    font-weight: 500;
    color: @gray-800;
  }
  .type-count {
    font-size: 0.875rem;
    color: @gray-600;
  }
}

.type-percentage {
  font-weight: 600;
  color: @primary;
  min-width: 48px;
  text-align: right;
}

.difficulty-distribution {
  .difficulty-bar {
    height: 8px;
    background-color: @gray-200;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 1rem;
    display: flex;

    .difficulty-segment {
      height: 100%;
      transition: width 0.3s ease;

      &.easy {
        background-color: #10b981;
      }

      &.medium {
        background-color: #f59e0b;
      }

      &.hard {
        background-color: #ef4444;
      }
    }
  }

  .difficulty-legend {
    display: flex;
    justify-content: space-between;

    .legend-item {
      display: flex;
      align-items: center;
      gap: 0.5rem;

      .legend-dot {
        width: 12px;
        height: 12px;
        border-radius: 50%;

        &.easy {
          background-color: #10b981;
        }

        &.medium {
          background-color: #f59e0b;
        }

        &.hard {
          background-color: #ef4444;
        }
      }

      .legend-text {
        font-size: 0.875rem;
        color: @gray-600;
      }

      .legend-percentage {
        font-weight: 500;
        color: @gray-800;
      }
    }
  }
}
</style>